package md.lanjing.com.module_home.ui.activity;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.lxh.baselibray.constant.ARouterConfig;
import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.ARouterUtils;
import com.lxh.baselibray.util.Md5Utils;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.RegexUtils;
import com.lxh.baselibray.util.SPUtils;
import com.lxh.baselibray.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;


import com.lxh.baselibray.constant.Constant;

import md.lanjing.com.module_home.R;
import md.lanjing.com.module_home.R2;
import md.lanjing.com.module_home.bean.LoginResponse;
import md.lanjing.com.module_home.contact.LoginContact;
import retrofit2.Response;

/**
 * 登录
 */
@Route(path = ARouterConfig.Home_login_act)
public class LoginActivity extends MvpActivity<LoginContact.LoginPresent> implements LoginContact.ILoginView{

    @BindView(R2.id.et_tel)
    EditText etTel;
    @BindView(R2.id.et_pwd)
    EditText etPwd;

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }


    @OnClick({R2.id.tv_forget_pwd, R2.id.tv_register, R2.id.btn_login})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.tv_forget_pwd) {
            startActivity(new Intent(this, ForgetLoginPwdActivity.class));

        } else if (i == R.id.tv_register) {
            startActivity(new Intent(this, RegisterActivity.class));

        } else if (i == R.id.btn_login) {
            String phone = etTel.getText().toString();
            String pwd = etPwd.getText().toString();
            if (validateLogin(phone, pwd)) return;

            showLoadingDialog();
            ARouterUtils.jump(ARouterConfig.MAIN_act);
     //       mPresent.login(context, phone, Md5Utils.stringToMD5(pwd));

        }
    }

    @Override
    public void getLoginData(Response<LoginResponse> response) {
        dismissLoadingDialog();
        if (response.body().getCode()== Constant.SUCCESS_CODE){
            ToastUtils.showLongToast(context,"登录成功");
            LoginResponse.DataBean data = response.body().getData();
            if (!ObjectUtils.isEmpty(data)){
                    SPUtils.putInt(Constant.USER_ID,data.getId(),context);
                    SPUtils.putString(Constant.PHONE,data.getMobilePhone(),context);
                    SPUtils.putString(Constant.INVITE_CODE,data.getInvitationCode(),context);
                    SPUtils.putString(Constant.IS_REAL,data.getRealName(),context);
                    SPUtils.putString(Constant.LOGIN_PWD,data.getLoginPassword(),context);
            }
            ARouterUtils.jump(ARouterConfig.MAIN_act);
            finish();
        }else {
            ToastUtils.showLongToast(context,response.body().getMsg());
        }

    }


    @Override
    protected LoginContact.LoginPresent createPresent() {
        return new LoginContact.LoginPresent();
    }

    private boolean validateLogin(String phone, String pwd) {
        if (ObjectUtils.isEmpty(phone)){
            ToastUtils.showLongToast(context,"手机号不能为空");
            return true;
        }
        if (!RegexUtils.isMobileSimple(phone)){
            ToastUtils.showLongToast(context,"手机号不正确");
            return true;
        }
        if (ObjectUtils.isEmpty(pwd)){
            ToastUtils.showLongToast(context,"密码不能为空");
            return true;
        }
        return false;
    }
}
